package com.itheima.day01.managerTest.service;

import com.itheima.day01.managerTest.controller.TeacherController;
import com.itheima.day01.managerTest.dao.TeacherDao;
import com.itheima.day01.managerTest.domain.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherService {
    TeacherDao teacherDao = new TeacherDao();

    // 招聘老师
    public boolean addTeacher(Teacher addTea) {
        boolean boo =teacherDao.addTeacher(addTea);
        return boo;
    }

    // 解雇老师
    public void deleteTeacherById(int index) {
        teacherDao.deleteTeacherById(index);
    }

    // 修改老师
    public void updateTeacher(int index, String id) {
        TeacherController teacherController = new TeacherController();
        Scanner sc = new Scanner(System.in);

        System.out.println("请问是否要修改该教师工号？,是：\"Y\" 否：任意键");
        String judge = sc.next();

        if("Y".equals(judge) || "y".equals(judge)){
            while (true){
                System.out.println("请录入老师新工号:");
                id = sc.next();
                int indexx = seekTeacherId(id);
                if(indexx == -1){
                    break;
                }else {
                    System.out.println("该工号已被占用，请重新录入");
                    System.out.println("如想退出请按“0”，继续请按任意数字键。");
                }
                String jud = sc.next();
                if("0".equals(jud)){
                    return;
                }
            }
        }

        Teacher teacher = teacherController.setTeacher(id);
        teacherDao.updateTeacher(index, teacher);

    }

    // 判断集合是否为空
    public boolean judgeListTea(){
        ArrayList<Teacher> listTea = teacherDao.findAllTeacher();
        if(listTea.size() == 0){
            return false;
        }
        return true;
    }

    // 返回老师集合
    public ArrayList<Teacher> findAllTeacher() {
        ArrayList<Teacher> listTea = teacherDao.findAllTeacher();
        return listTea;
    }

    // 通过传入老师工号，找出老师信息索引
    public int seekTeacherId(String id){
        int index = -1;
        ArrayList<Teacher> listTea = teacherDao.findAllTeacher();
        for (int i = 0; i < listTea.size(); i++) {
            Teacher teacher = listTea.get(i);
            if(teacher.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }



}
